<?php

/**
 * ECSHOP 管理中心品牌管理
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: brand.php 17217 2011-01-19 06:29:08Z liubo $
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table("comparison"), $db, 'comparison_id', 'comparison_name');

if ($_REQUEST['act'] == 'list')
{
    $smarty->assign('ur_here',      $_LANG['comparison_list']);
    $smarty->assign('action_link',  array('text' => $_LANG['comparison_add'], 'href' => 'comparison.php?act=add'));
    $smarty->assign('full_page',    1);

    $comparison_list = get_comparisonlist();

    foreach($comparison_list['comparison'] as $key=>$vo){
        $goods_id=$vo['a_id'];
        $sql = "select goods_name from ".$ecs->table('goods')." where goods_id=".
            $goods_id;
        $comparison_list['comparison'][$key]['a_id'] = $db->getOne($sql);
        $goods_id=$vo['b_id'];
        $sql = "select goods_name from ".$ecs->table('comparison_goods')." where goods_id=".
            $goods_id;
        $comparison_list['comparison'][$key]['b_id'] = $db->getOne($sql);
    }

    $smarty->assign('comparison_list',   $comparison_list['comparison']);
    $smarty->assign('filter',       $comparison_list['filter']);
    $smarty->assign('record_count', $comparison_list['record_count']);
    $smarty->assign('page_count',   $comparison_list['page_count']);

    assign_query_info();
    $smarty->display('comparison_list.htm');

}
/*------------------------------------------------------ */
//-- 添加对比品牌
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    //admin_priv('brand_manage');
    include_once(ROOT_PATH . 'includes/fckeditor/fckeditor.php'); // 包含 html editor 类文件
    /* 创建 html editor */
    create_html_editor('ab_desc', '');

    $smarty->assign('ur_here',     $_LANG['comparison_add']);
    $smarty->assign('action_link', array('text' => $_LANG['comparison_list'], 'href' => 'comparison.php?act=list'));
    $smarty->assign('form_action', 'insert');

    $sql = "select brand_name, brand_id from ".$ecs->table('brand');
    $a_brand = $db->getAll($sql);
    $smarty->assign('a_brand',$a_brand);

    $sql = "select brand_name, brand_id from ".$ecs->table('comparison_brand');
    $b_brand = $db->getAll($sql);
    $smarty->assign('b_brand',$b_brand);

    assign_query_info();
    $smarty->display('comparison_add.htm');
}
elseif ($_REQUEST['act'] == 'brand_change')
{
    if($_REQUEST['a_brand_id']){
        $brand_id=$_REQUEST['a_brand_id'];
        $sql = "select goods_id, goods_name from ".$ecs->table('goods')." where brand_id=".$brand_id;
        $my_goods = $db->getAll($sql);
        $options="";
        foreach($my_goods as $k=>$v){
            $options .="<option value='".$v['goods_id']."'>".$v['goods_name']."</option>";
        }
        echo $options;
    }
    if($_REQUEST['b_brand_id']){
        $brand_id=$_REQUEST['b_brand_id'];
        $sql = "select goods_id, goods_name from ".$ecs->table('comparison_goods')." where brand_id=".$brand_id;
        $other_goods = $db->getAll($sql);
        $options="";
        foreach($other_goods as $k=>$v){
            $options .="<option value='".$v['goods_id']."'>".$v['goods_name']."</option>";
        }
        echo $options;
    }
}
elseif ($_REQUEST['act'] == 'insert')
{
    /*检查品牌名是否重复*/
   // admin_priv('brand_manage');
    $comparison_name = empty($_REQUEST['comparison_name'])?'':$_REQUEST['comparison_name'];
    $a_brand_id = empty($_REQUEST['a_brand_id'])?'':$_REQUEST['a_brand_id'];
    $a_id = empty($_REQUEST['a_id'])?'':$_REQUEST['a_id'];
    $b_brand_id = empty($_REQUEST['b_brand_id'])?'':$_REQUEST['b_brand_id'];
    $b_id = empty($_REQUEST['b_id'])?'':$_REQUEST['b_id'];
    $ab_desc = empty($_REQUEST['ab_desc'])?'':$_REQUEST['ab_desc'];
    if($comparison_name == ''){
        sys_msg('产品对比标题不能为空！',1);
    }
    if($a_brand_id == ''){
        sys_msg('请先选择本店品牌！',1);
    }
    if($a_id == ''){
        sys_msg('请先选择本店商品！',1);
    }
    if($b_brand_id == ''){
        sys_msg('请先选择对比品牌！',1);
    }
    if($b_id == ''){
        sys_msg('请先选择对比商品！',1);
    }
    if($ab_desc == ''){
        sys_msg('编辑对比内容不能为空！',1);
    }
    $sql = "select goods_brief from ".$ecs->table('goods')." where goods_id=".$a_id;
    $goods_brief = $db->getOne($sql);


    $sql = "INSERT INTO ".$ecs->table('comparison')."(comparison_name, a_id, a_brief, a_brand_id, b_id, b_brand_id, ab_desc) ".
        "VALUES ('$comparison_name', '$a_id', '$goods_brief', '$a_brand_id', '$b_id', '$b_brand_id', '$ab_desc')";

    $db->query($sql);

    admin_log($_POST['comparison_name'],'add','comparison');

    /* 清除缓存 */
    clear_cache_files();

    $link[0]['text'] = "继续添加产品对比";
    $link[0]['href'] = 'comparison.php?act=add';

    $link[1]['text'] = '返回产品对比列表';
    $link[1]['href'] = 'comparison.php?act=list';

    sys_msg('添加对比记录成功！', 0, $link);
}

/*------------------------------------------------------ */
//-- 编辑品牌
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    //admin_priv('brand_manage');
    include_once(ROOT_PATH . 'includes/fckeditor/fckeditor.php'); // 包含 html editor 类文件

    $sql = "SELECT comparison_id, comparison_name, a_id, a_brand_id, b_id, b_brand_id, ab_desc ".
        "FROM " .$ecs->table('comparison'). " WHERE comparison_id='$_REQUEST[id]'";
    $comparison = $db->GetRow($sql);

    $smarty->assign('ur_here',     '修改产品对比信息');
    $smarty->assign('action_link', array('text' => $_LANG['comparison_list'], 'href' => 'comparison.php?act=list&' . list_link_postfix()));
    $smarty->assign('comparison',       $comparison);
    /* 创建 html editor */
    create_html_editor('ab_desc', $comparison['ab_desc']);
    $smarty->assign('form_action', 'updata');

    $sql = "select brand_name, brand_id from ".$ecs->table('brand');
    $a_brand = $db->getAll($sql);
    $smarty->assign('a_brand',$a_brand);

    $sql = "select goods_id, goods_name from ".$ecs->table('goods')." where brand_id=".$comparison['a_brand_id'];
    $my_goods = $db->getAll($sql);
    $smarty->assign('my_goods',$my_goods);

    $sql = "select brand_name, brand_id from ".$ecs->table('comparison_brand');
    $b_brand = $db->getAll($sql);
    $smarty->assign('b_brand',$b_brand);

    $sql = "select goods_id, goods_name from ".$ecs->table('comparison_goods')." where brand_id=".$comparison['b_brand_id'];
    $other_goods = $db->getAll($sql);
    $smarty->assign('other_goods',$other_goods);

    assign_query_info();
    $smarty->display('comparison_add.htm');
}
elseif ($_REQUEST['act'] == 'updata')
{
    //admin_priv('brand_manage');
    $comparison_name = empty($_REQUEST['comparison_name'])?'':$_REQUEST['comparison_name'];
    $a_brand_id = empty($_REQUEST['a_brand_id'])?'':$_REQUEST['a_brand_id'];
    $a_id = empty($_REQUEST['a_id'])?'':$_REQUEST['a_id'];
    $b_brand_id = empty($_REQUEST['b_brand_id'])?'':$_REQUEST['b_brand_id'];
    $b_id = empty($_REQUEST['b_id'])?'':$_REQUEST['b_id'];
    $ab_desc = empty($_REQUEST['ab_desc'])?'':$_REQUEST['ab_desc'];
    if($comparison_name == ''){
        sys_msg('产品对比标题不能为空！',1);
    }
    if($a_brand_id == ''){
        sys_msg('请先选择本店品牌！',1);
    }
    if($a_id == ''){
        sys_msg('请先选择本店商品！',1);
    }
    if($b_brand_id == ''){
        sys_msg('请先选择对比品牌！',1);
    }
    if($b_id == ''){
        sys_msg('请先选择对比商品！',1);
    }
    if($ab_desc == ''){
        sys_msg('编辑对比内容不能为空！',1);
    }

    $param = "comparison_name = '$comparison_name',  a_id='$a_id', a_brand_id='$a_brand_id', b_id='$b_id', b_brand_id='$b_brand_id' ";

    if ($exc->edit($param,  $_POST['id']))
    {
        /* 清除缓存 */
        clear_cache_files();

        admin_log($_POST['comparison_name'], 'edit', 'comparison');

        $link[0]['text'] = '返回产品对比列表';
        $link[0]['href'] = 'comparison.php?act=list&' . list_link_postfix();
        $note = vsprintf("修改成功！", $_POST['comparison_name']);
        sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}

/*------------------------------------------------------ */
//-- 编辑品牌名称
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_comparison_name')
{
    //check_authz_json('brand_manage');

    $id     = intval($_POST['id']);
    $name   = json_str_iconv(trim($_POST['val']));


        if ($exc->edit("comparison_name = '$name'", $id))
        {
            admin_log($name,'edit','comparison');
            make_json_result(stripslashes($name));
        }
        else
        {
            make_json_result(sprintf("修改出错！", $name));
        }

}

/*------------------------------------------------------ */
//-- 删除对比信息
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    //check_authz_json('brand_manage');

    $id = intval($_GET['id']);

    $exc->drop($id);

    $url = 'comparison.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $comparison_list = get_comparisonlist();
    foreach($comparison_list['comparison'] as $key=>$vo){
        $goods_id=$vo['a_id'];
        $sql = "select goods_name from ".$ecs->table('goods')." where goods_id=".
            $goods_id;
        $comparison_list['comparison'][$key]['a_id'] = $db->getOne($sql);
        $goods_id=$vo['b_id'];
        $sql = "select goods_name from ".$ecs->table('comparison_goods')." where goods_id=".
            $goods_id;
        $comparison_list['comparison'][$key]['b_id'] = $db->getOne($sql);
    }
    $smarty->assign('comparison_list',   $comparison_list['comparison']);
    $smarty->assign('filter',       $comparison_list['filter']);
    $smarty->assign('record_count', $comparison_list['record_count']);
    $smarty->assign('page_count',   $comparison_list['page_count']);

    make_json_result($smarty->fetch('comparison_list.htm'), '',
        array('filter' => $comparison_list['filter'], 'page_count' => $comparison_list['page_count']));
}

/**
 * 获取品牌列表
 *
 * @access  public
 * @return  array
 */
function get_comparisonlist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        if(strtoupper(EC_CHARSET) == 'GBK')
        {
            $keyword = iconv("UTF-8", "gb2312", $_POST['comparison_name']);
        }
        else
        {
            $keyword = $_POST['comparison_name'];
        }
        /* 记录总数以及页数 */
        if (isset($_POST['comparison_name']))
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('comparison') . " WHERE comparison_name like '%{$keyword}%'";
        }
        else
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('comparison');
        }

        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 */
        if (isset($_POST['comparison_name']))
        {
            if(strtoupper(EC_CHARSET) == 'GBK')
            {
                $keyword = iconv("UTF-8", "gb2312", $_POST['comparison_name']);
            }
            else
            {
                $keyword = $_POST['comparison_name'];
            }
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('comparison')." WHERE comparison_name like '%{$keyword}%'";
        }
        else
        {
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('comparison');
        }

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql."  order by comparison_id desc", $filter['page_size'], $filter['start']);

    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $rows['date'] = local_date($GLOBALS['_CFG']['time_format'], $rows['add_time']);

        $arr[] = $rows;
    }

    return array('comparison' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

?>